﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;

namespace DataManager.DAL
{
    public struct Director
    {
        int m_DirectorId;
        string m_DirectorName;
        string m_Nationality;

        public int DirectorId
        {
            get { return m_DirectorId; }
            set { m_DirectorId = value; }
        }

        public string DirectorName
        {
            get { return m_DirectorName; }
            set { m_DirectorName = value; }
        }

        public string Nationality
        {
            get { return m_Nationality; }
            set { m_Nationality = value; }
        }
    }
    class Directors:DAL
    {
        /// <summary>
        /// Visszateriti a rendezok listajat kereses szerint, ha nincs parameter akkor az osszeset
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<Director> GetDirectorList(string name)
        {
            string query = "SELECT RendezoId, RendezoNev, Nemzetiseg " +
                "FROM Rendezok ";
            if (name != "")
            {
                query = query + "WHERE RendezoNev LIKE '%" + name + "%'";
            }
            string error = string.Empty;
            SqlDataReader rdr = ExecuteReader(query, ref error);
            List<Director> directorList = new List<Director>();
            if (error == "OK")
            {
                while (rdr.Read())
                {
                    Director item = new Director();
                    item.DirectorId = Convert.ToInt32(rdr["RendezoId"]);
                    item.DirectorName = rdr["RendezoNev"].ToString();
                    item.Nationality = rdr["Nemzetiseg"].ToString();
                    directorList.Add(item);
                }
            }
            CloseDataReader(rdr);
            return directorList;
        }

        /// <summary>
        /// Beszur egy rendezot, tarolt eljaras hasznalataval
        /// </summary>
        /// <param name="name"></param>
        /// <param name="nationality"></param>
        /// <returns></returns>
        public string InsertDirector(string name, string nationality)
        {
            string[] parameterNames = new string[2], parameterValues = new string[2];
            parameterNames[0] = "@Nev"; parameterValues[0] = name.ToString();
            parameterNames[1] = "@Nemzetiseg"; parameterValues[1] = nationality.ToString();

            return ExecuteStoredProcedureNonQuery("UjRendezo", parameterNames, parameterValues);
        }

        /// <summary>
        /// Modositja a megfelelo Id-ju rendezo adatait
        /// </summary>
        /// <param name="directorId"></param>
        /// <param name="name"></param>
        /// <param name="nationality"></param>
        /// <returns></returns>
        public string UpdateDirector(int directorId, string name, string nationality)
        {
            string query = "UPDATE Rendezok " +
                "SET RendezoNev = '" + name + "', " +
                "Nemzetiseg = '" + nationality + "' " +
                "WHERE RendezoId = " + directorId;
            return ExecuteNonQuery(query);
        }

        /// <summary>
        /// Torli a megadott Id-ju rendezot
        /// </summary>
        /// <param name="directorId"></param>
        /// <returns></returns>
        public string DeleteDirector(int directorId)
        {
            string query = "DELETE FROM Rendezok WHERE RendezoId = " + directorId;
            return ExecuteNonQuery(query);
        }
    }
}
